package gui2;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;

import schedule.Appointment;
import schedule.AppointmentList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;

public class AppointmentPanel extends JPanel {
	private JList<Appointment> jlist;
	private JButton btnEdit;
	private JButton btnClearList;
	private JButton btnDelete;
	
	private AppointmentList list;
	
	private MyListModel model;

	/**
	 * Create the panel.
	 */
	public AppointmentPanel(AppointmentList list) {
		this.list = list;
		
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.SOUTH);
		panel.setLayout(new GridLayout(0, 3, 0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1);
		
		btnClearList = new JButton("Clear List");
		btnClearList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnClearList_actionPerformed(arg0);
			}
		});
		panel_1.add(btnClearList);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2);
		
		btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				do_btnEdit_actionPerformed(e);
			}
		});
		panel_2.add(btnEdit);
		
		JPanel panel_4 = new JPanel();
		panel.add(panel_4);
		
		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnDelete_actionPerformed(arg0);
			}
		});
		panel_4.add(btnDelete);
		
		JPanel panel_3 = new JPanel();
		add(panel_3, BorderLayout.NORTH);
		
		JLabel lblNextAppointments = new JLabel("next appointments:");
		panel_3.add(lblNextAppointments);
		
		model = new MyListModel(list);
		
		jlist = new JList<Appointment>(model);
		jlist.setCellRenderer(new MyRenderer());
		
		JScrollPane scrollPane = new JScrollPane(jlist);
		add(scrollPane, BorderLayout.CENTER);
		scrollPane.setViewportView(jlist);
	}
	
	public void refresh(){
		jlist.updateUI();
	}
	
	protected void do_btnClearList_actionPerformed(ActionEvent arg0) {
		list.clearList();
		jlist.updateUI();
	}
	
	protected void do_btnEdit_actionPerformed(ActionEvent e) {
		EditWindow edit;
		try{
		if(!jlist.isSelectionEmpty())
			edit = new EditWindow(jlist.getSelectedValue(), list);
		else
			throw new Exception("no appointment selected");
		}catch(Exception ex){
			JOptionPane.showMessageDialog(this, ex.getMessage());
		}
	}
	
	protected void do_btnDelete_actionPerformed(ActionEvent arg0) {
		try{
			if(!jlist.isSelectionEmpty()){
				list.remove(jlist.getSelectedValue());
				jlist.updateUI();
			}else
				throw new Exception("no appointment selected");
			}catch(Exception ex){
				JOptionPane.showMessageDialog(this, ex.getMessage());
			}
	}
	
	
	private class MyListModel implements ListModel<Appointment>{
		private AppointmentList list;
		
		public MyListModel(AppointmentList list){
			this.list = list;
		}
		
		@Override
		public void addListDataListener(ListDataListener l) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Appointment getElementAt(int index) {
			return list.get(index);
		}

		@Override
		public int getSize() {
			return list.size();
		}

		@Override
		public void removeListDataListener(ListDataListener l) {
			// TODO Auto-generated method stub
			
		}
		
	}

	private class MyRenderer implements ListCellRenderer<Appointment>{

		@Override
		public Component getListCellRendererComponent(
				JList<? extends Appointment> jlist, Appointment value, int index,
				boolean selected, boolean focus) {
			JLabel label = new JLabel(""+ value);
			label.setOpaque(true);
			if(selected){
				label.setBackground(jlist.getSelectionBackground());
				label.setForeground(jlist.getSelectionForeground());
			}else{
				label.setBackground(jlist.getBackground());
				label.setForeground(jlist.getForeground());
			}
			return label; 
		}
		
	}
	
	public JList<Appointment> getJlist(){
		return jlist;
	}
}
